//
//  LCYChatMainCollectionViewCell.m
//  IMByLinkUs
//
//  Created by lcy on 16/1/21.
//  Copyright © 2016年 lcy. All rights reserved.
//

#import "LCYChatMainCollectionViewCell.h"
#import "LCYShapedImageView.h"
#import "NSDate+LCYCurrentDate.h"

@interface LCYChatMainCollectionViewCell ()

@property (nonatomic, strong) NSTimer *playProgress; // 显示播放进度

@end

@implementation LCYChatMainCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        // 发送时间
        self.sendTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(floor((kLCYScreenWidth - 200) * 0.5), 10, 200, 30)];
        self.sendTimeLabel.textAlignment = NSTextAlignmentCenter;
        self.sendTimeLabel.textColor = [UIColor lightGrayColor];
        self.sendTimeLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.sendTimeLabel];
        
        // 头像
        self.iconView = [[UIImageView alloc] initWithFrame:CGRectMake(floor(kLCYScreenWidth * 0.9 - 10), 50, floor(kLCYScreenWidth * 0.1), floor(kLCYScreenWidth * 0.1))];
        self.iconView.image = [UIImage imageNamed:@"icon"];
        [self.contentView addSubview:self.iconView];
        
        // 名字
        self.nameLabel = [[UILabel alloc] init];
        self.nameLabel.font = [UIFont systemFontOfSize:10];
        self.nameLabel.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:self.nameLabel];
        
        
    }
    return self;
}

- (void)setMyMessage:(LCYIMMessage *)myMessage
{
    _myMessage = [myMessage copy];
    _cellType = myMessage.cellType;
    
    // 气泡
    
    _bubbleView = [[LCYShapedImageView alloc] init];
    
    // 添加轻拍手势
    UITapGestureRecognizer *tapTextView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickedBtn)];
    [self.bubbleView.myTextView addGestureRecognizer:tapTextView];
    
    // 展示时间
    self.sendTimeLabel.text = [NSDate stringFromDate:myMessage.date];
    
    _bubbleView.myMessage = myMessage;
    [_bubbleView setImage:[UIImage imageNamed:@"bubble_background"]];
    
    // 昵称
    self.nameLabel.text = myMessage.senderDisplayName;
    
    // 获取size
    CGSize bubbleSize = _bubbleView.sizeOfBubble;
    CGFloat beakWidth = _bubbleView.beakWidth;
    
    // 计算行高
    _cellHeight = 50 + bubbleSize.height;
    switch (myMessage.cellType) {
        case PostCell:
        {
            
            [self initPostBubble:bubbleSize beakWidth:beakWidth];
        }
            break;
        case GetCell:
        {
            
        }
            break;
        case GetAudioCell:
            
            break;
        case PostAudioCell:
        {
            [self initPostBubble:bubbleSize beakWidth:beakWidth];
            
            [self initPlayHint];
        }
            break;
        case PostImageCell:
            
            break;
        case GetImageCell:
        {
            
        }
            break;
        case PostVideoCell:
            
            break;
        case GetVideoCell:
        {
            
        }
            break;
        case PostFileCell:
            
            break;
        case GetFileCell:
        {
            
        }
            break;
        case PostLocationCell:
            
            break;
        case GetLocationCell:
        {
            
        }
            break;
        default:
            break;
    }
    
    for(int i = 0; i < self.contentView.subviews.count; i ++)
    {
        id view = self.contentView.subviews[i];
        if([[view class] isSubclassOfClass:[LCYShapedImageView class]])
        {
            LCYShapedImageView *shapeView = (LCYShapedImageView *)view;
            if(![shapeView.myTextView.attributedText
                
                isEqualToAttributedString:
                
                [[NSAttributedString alloc] initWithString:myMessage.text attributes:nil]])
            {
                shapeView.myTextView.attributedText = [[NSAttributedString alloc] initWithString:myMessage.text attributes:nil];
            }
            break;
        }
        
        if(i == self.contentView.subviews.count - 1)
        {
            [self.contentView addSubview:_bubbleView];
        }
    }
}

// 初始化post气泡
- (void)initPostBubble:(CGSize)bubbleSize beakWidth:(CGFloat)beakWidth
{
    self.nameLabel.frame = CGRectMake(floor(kLCYScreenWidth * 0.9 - 20 - 200), 30, 200, 20);
    
    _bubbleView.frame = CGRectMake(floor(kLCYScreenWidth * 0.9 - 10 - bubbleSize.width - beakWidth), 50, floor(bubbleSize.width), floor(bubbleSize.height));
}

// 轻拍
- (void)clickedBtn
{
    // 让代理播放对应音频文件
    if(_cellType == PostAudioCell)
    {
        if(self.delegate && [self.delegate respondsToSelector:@selector(tapToPlayAudioWithFileName:)])
        {
            [self.delegate tapToPlayAudioWithFileName:self.indexPath];
            
            self.playHint.hidden = NO;
            
            self.audioDuration = [self.myMessage.text copy];
            
            _playProgress = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(didPlayAudio) userInfo:nil repeats:YES];
        }
    }else if(_cellType == PostVideoCell)
    {
        if(self.delegate && [self.delegate respondsToSelector:@selector(tapToPlayVideoWithFileURL:)])
        {
            [self.delegate tapToPlayVideoWithFileURL:self.indexPath];
        }
    }
    
}

// 初始化播放提示
- (void)initPlayHint
{
//    self.playHint = [[UILabel alloc] initWithFrame:CGRectMake(self.bubbleView.frame.origin.x - 150, 50, 150, 20)];
    // 初始化播放提示信息
    self.playHint.textAlignment = NSTextAlignmentRight;
    self.playHint.text = @"正在播放录音...";
    [self.contentView addSubview:self.playHint];
    self.playHint.hidden = YES;
}

// 刷新播放进度
- (void)didPlayAudio
{
    NSInteger restTime = [self.bubbleView.myTextView.text integerValue];
    
    if(restTime == 0)
    {
        [_playProgress invalidate];
        
        self.playHint.hidden = YES;
        
        self.bubbleView.myTextView.text = [self.audioDuration copy];
        return;
    }
    
//    self.bubbleView.myTextView.attributedText = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%ld'", restTime - 1] attributes:nil];
    
    self.bubbleView.myTextView.text = [NSString stringWithFormat:@"%ld'", restTime - 1];
}

@end
